package com.example.lib_mvp.views;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListPopupWindow;
import android.widget.TextView;

import com.example.lib_mvp.beans.AiDrawSizeBean;
import com.example.lib_mvp.R;

import java.util.List;

public class ListPopupWindowUtils {


    public static void showListPop(Context context, List<AiDrawSizeBean> data, View anchorView,
                                   AdapterView.OnItemClickListener clickListener) {
        //初始化ListPopupWindow，适配
        ListPopupWindow listPopupWindow = new ListPopupWindow(context);
        //自定义适配器，布局
        MyApader adapter = new MyApader(context, data);
        listPopupWindow.setAdapter(adapter);
        listPopupWindow.setAnchorView(anchorView);//设置ListPopupWindow的锚点，关联mButton位置
        listPopupWindow.setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
        listPopupWindow.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
        listPopupWindow.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.hd_shape_page_pop_top_juxing));
        listPopupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                clickListener.onItemClick(parent, view, position, id);
                listPopupWindow.dismiss(); //关闭listPopupWindow
            }
        });
        listPopupWindow.show();
    }

    public static class MyApader extends BaseAdapter {
        private Context context;
        private List<AiDrawSizeBean> mList;

        public MyApader(Context context, List<AiDrawSizeBean> mList) {
            this.context = context;
            this.mList = mList;
        }

        @Override
        public int getCount() {
            return mList.size();
        }

        @Override
        public Object getItem(int position) {
            return mList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (null == convertView) {
                convertView = View.inflate(context, R.layout.hd_ai_item_draw_size, null);
            }
            TextView textView = (TextView) convertView.findViewById(R.id.ai_draw_size_name_tv);
            textView.setText(mList.get(position).getSizeName());

            View ai_draw_size_name_line_view = convertView.findViewById(R.id.ai_draw_size_name_line_view);
            ai_draw_size_name_line_view.setVisibility(position != mList.size() - 1 ? View.VISIBLE : View.GONE);
            return convertView;
        }
    }
}
